const util = require('../../utils/util');
const API = require('../../common/constant');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bussData: [],
    noData: false
  },
  toPersonInfo: function (e) {
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '../FanInfo/PersonInfo/PersonInfo?id=' + e.currentTarget.id
    })
  },
  /**
   * 获取同意列表列表
   */
  getEvents: function (id) {
    util.http({
      url: API.URL.EVENT.REGISTRATIONMANAGEMENT,
      data: {
        idEventInfoId: id,
        pageIndex: 1,
        pageSize: 10
      },
      success: res => {
        console.log(res.data.bussData);
        if (res.data.bussData.length == 0) {
          this.setData({
            noData: true
          });
        } else {
          this.setData({
            bussData: res.data.bussData
          })
        }
      },
      fail: rej => {
        console.log(rej);
      }
    })
  },
  /**
   * 同意报名
   */
  confirmSign: function (e) {
    if (this.data.status === '已结束') {
      app.showUserInfo('活动已结束', 'none', 2000)
      return
    }
    let id = e.currentTarget.id;
    let idEventInfoId = e.currentTarget.dataset.hi;
    let that = this;
    util.http({
      url: API.URL.EVENT.FIND_EVENTINFODETAILBYID,
      data: {
        id: idEventInfoId
      },
      success: res => {
        console.log(res);
        res.data.bussData.registrationSuccessCount;
        if (res.data.bussData.registrationSuccessCount < res.data.bussData.eventNumber) {
          util.http({
            url: API.URL.REGISTRATION.EDIT_EVENTREGISTRATION,
            data: {
              id: id,
              idEventInfoId: idEventInfoId
            },
            success: res => {
              console.log(res);
              app.globalData.isNewIndex = true;
              app.globalData.isNewNearBy = true;
              that.getEvents(idEventInfoId);
            },
            fail: rej => {
              console.log(rej);
            }
          });
        }
        else {
          app.showUserInfo('该活动报名人数已满', 'none', 1500);
        }
      },
      fail: err => {
        console.log(err);
        app.showUserInfo('同意失败', 'none', 1500);
      }
    })
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    let status = options.status || false
    this.setData({
      status: status
    })
    this.getEvents(options.id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})